<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead('文件上传-小邑')"/>
    <style>
        #formAdvForm .layui-row {
            margin-top: 20px;
        }

        #formAdvForm .layui-form-item {
            margin-bottom: 0;
        }

        #formAdvForm .layui-form-item .layui-inline {
            margin-bottom: 25px;
            margin-right: 0;
        }
    </style>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<script>
    var parentId = null;
    var filePath = null;
    $(function () {
        parentId = utils.request.getQueryString("parentId");
        filePath = parent.filePath
    })
</script>
<body>
<form class="layui-form" action="" id="formAdvForm" lay-filter="formAdvForm">
    <div class="layui-fluid layui-fluid-btn">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-upload">
                    <button type="button" class="xy-btn xy-btn-md xy-btn-primary" id="testList">选择多文件</button>
                    <div class="layui-upload-list" style="max-width: 1000px;">
                        <table class="layui-table">
                            <colgroup>
                                <col>
                                <col width="150">
                                <col width="260">
                                <col width="150">
                            </colgroup>
                            <thead>
                            <tr>
                                <th>文件名</th>
                                <th>大小</th>
                                <th>上传进度</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="demoList"></tbody>
                        </table>
                    </div>
                </div>
                <div class="form-group-bottom">
                    <button class="xy-btn xy-btn-primary" type="button" id="testListAction">开始上传</button>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    layui.use(['layer', 'jquery', 'form', 'notice','element', 'upload'], function () {
        var $ = layui.jquery
            , form = layui.form
            , notice = layui.notice
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;
        element.init();
        //多文件列表
        var uploadListIns = upload.render({
            elem: '#testList'
            , elemList: $('#demoList') //列表元素对象
            , url: basePath+"/file/addFile?parentId="+parentId+"&filePath="+filePath //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            , accept: 'file'
            , multiple: true
            , number: 5 //设置同时可上传的文件数量
            , size: 1024*1024*200 //设置文件最大可允许上传的大小，单位 KB。 200MB
            , auto: false
            , bindAction: '#testListAction'
            , choose: function (obj) {
                var that = this;
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    if ($("#testListAction").hasClass("layui-btn-disabled")){
                        $("#testListAction").removeClass("layui-btn-disabled");
                    }
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td style="word-break: break-word">' + file.name + '</td>'
                        , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                        , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                        , '<td>'
                        , '<button class="xy-btn xy-btn-xs xy-reload layui-hide" >重传</button>'
                        , '<button class="xy-btn xy-btn-xs xy-btn-danger xy-delete" style="margin-left: 3px;">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.xy-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.xy-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    that.elemList.append(tr);
                    element.render('progress'); //渲染新加的进度条组件
                });
            }
            , done: function (res, index, upload) { //成功的回调
                element.init();//上传结束后 进度条已经不在变化 所以仍需要为下一个事件初始化否则进度条仍然为满状态 但数据为0%
                var that = this;
                layer.closeAll('loading');
                if(res.success){ //上传成功
                    var tr = that.elemList.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , allDone: function (obj) { //多文件上传完毕后的状态回调
                $("#testListAction").addClass("layui-btn-disabled");
            }
            , error: function (index, upload) { //错误回调
                var that = this;
                var tr = that.elemList.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(3).find('.xy-reload').removeClass('layui-hide'); //显示重传
            }
            ,before: function(obj){
                layer.load(1); //上传loading
            }
            , progress: function (n, elem, e, index) { //注意：index 参数为 layui 2.6.6 新增
                element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
            }
        });
    });
</script>
</body>
</html>
